package com.sw.chinesewriteboard.activity;

import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sw.chinesewriteboard.R;
import com.sw.chinesewriteboard.config.ChineseDataConfig;
import com.sw.chinesewriteboard.copybook.ZhCopybookActivity;
import com.sw.chinesewriteboard.operation.ChineseDataOperate;

/**
 * @Description:
 * @author: GordenGao
 * @Email: gordengao124@gmail.com
 * @date: 2021年06月23日 9:33
 */
public class ZhNameCopybookActivity extends ZhCopybookActivity {

    @Override
    protected void initOwnView() {
//        setTitle("姓名字帖");
        ChineseDataConfig.setRow(8);
        binding.vbnBottomNavigation.initEnvironment(this, true);
        binding.vbnBottomNavigation.setContantHint("请输入姓名。例如：英文字帖");
        binding.vbnBottomNavigation.findViewById(R.id.vbn_gezi_csl_kuandu).setVisibility(View.GONE);
        binding.vbnBottomNavigation.findViewById(R.id.vbn_gezi_csl_kongbaihang).setVisibility(View.GONE);
        binding.vbnBottomNavigation.findViewById(R.id.vbn_gezi_csl_miao).setVisibility(View.GONE);
        binding.vbnBottomNavigation.findViewById(R.id.vbn_ziti_ll_zihao).setVisibility(View.GONE);
        binding.vbnBottomNavigation.findViewById(R.id.vbn_ziti_ll_alpha).setVisibility(View.GONE);
    }

    @Override
    protected boolean isPinyinEnabled() {
        return true;
    }

    @Override
    protected boolean needInitDir() {
        return true;
    }

    @Override
    protected ChineseDataOperate.ArrangeType setArrangeType() {
        return ChineseDataOperate.ArrangeType.NAME;
    }

    @Override
    public void refreshCopyBookView() {
        int COLUMN_LENGTH = ChineseDataConfig.TOTAL_LENGTH_COLUMN;
        int ROW_LENGTH = ChineseDataConfig.TOTAL_LENGTH_ROW;

        mWordPaper = new LinearLayout(this);
        mWordPaper.setLayoutParams(new LinearLayout.LayoutParams(ChineseDataConfig.getA4width(this), ChineseDataConfig.getA4height(this)));
        mWordPaper.setGravity(Gravity.CENTER);
        mWordPaper.setAlpha(0f);
        binding.getRoot().addView(mWordPaper);

        mWordList = new LinearLayout(this);
        mWordList.setLayoutParams(new LinearLayout.LayoutParams(WRC, WRC));
        mWordList.setOrientation(LinearLayout.VERTICAL);
        mWordList.setGravity(Gravity.CENTER);
        mWordList.removeAllViews();

        mEditTextList.clear();

        mData.setText(mChineseDataOperate.listToString(ChineseDataOperate.ArrangeType.NAME, mListContent));

        int startIndex = ChineseDataConfig.TOTAL_LENGTH * mPage;    // 字符串的起始坐标
        int k = startIndex;
        System.out.println("日志：p：" + mPage + "    k:" + k);

        LinearLayout nameRow = new LinearLayout(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WRC, WRC);
        layoutParams.bottomMargin = 130;
        nameRow.setLayoutParams(layoutParams);
        nameRow.setOrientation(LinearLayout.HORIZONTAL);
        String name;
        int nameLength;
        if (!mListContent.isEmpty()) {
            name = mListContent.get(0);
        } else {
            name = "  ";
        }
        nameLength = name.length();
        for (int i = 0; i < nameLength; i++) {
            if (i == 0) {
                mCharacterOperation.setIndex(mCharacterOperation.LEFTMOST);
            } else if (i == nameLength - 1) {
                mCharacterOperation.setIndex(mCharacterOperation.RIGHTMOST);
            } else {
                mCharacterOperation.setIndex(mCharacterOperation.NORMAL);
            }
            View view = createCharacterLayout(name.charAt(i), mData);
            view.setLayoutParams(new LinearLayout.LayoutParams(WRC, WRC));
            nameRow.addView(view);
        }
        mWordList.addView(nameRow);

        TextView textView = new TextView(this);
        textView.setText("#姓名和拼音描写#");
        textView.setTextSize(58);
        mWordList.addView(textView, new LinearLayout.LayoutParams(WRC, WRC));


        for (int i = 0; i < ROW_LENGTH; i++) {
            LinearLayout row = new LinearLayout(this);
            row.setLayoutParams(new LinearLayout.LayoutParams(WRC, WRC));
            row.setOrientation(LinearLayout.HORIZONTAL);
            for (int j = 0; j < COLUMN_LENGTH; j++) {
                if (j == 0) {
                    mCharacterOperation.setIndex(mCharacterOperation.LEFTMOST);
                } else if (j == COLUMN_LENGTH - 1) {
                    mCharacterOperation.setIndex(mCharacterOperation.RIGHTMOST);
                } else {
                    mCharacterOperation.setIndex(mCharacterOperation.NORMAL);
                }
                View view = createCharacterLayout(mData.getText().charAt(k), mData);
                view.setLayoutParams(new LinearLayout.LayoutParams(WRC, WRC));
                row.addView(view);
                k++;
            }
            System.out.println("GGGG" + row.getWidth());
            mWordList.addView(row);
        }
        mWordPaper.addView(mWordList);

        binding.cbIvCopybook.post(new Runnable() {
            @Override
            public void run() {
                binding.cbIvCopybook.setImageBitmap(loadBitmapFromView());
                binding.getRoot().removeView(mWordPaper);
            }
        });
    }
}
